// formPackage/pages/platformModule/createNewCommissionRule/createNewCommissionRule.js

import {
  queryOneCommissionRuleDetail,
  createOneCommissionRule,
  updateOneCommissionRule
} from "../../../../service/platform_service"
import debounce from "../../../../tools/debounce"
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    formData: {
      "attention": "",
      "buyRule": "",
      "commissionRule": "",
      "lookRule": "",
      "name": "",
      "recommendRule": "",
      "settlementPeriod": "",

    },
    bottomBtnTitle: "创建规则",
    detailId: "",

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.setData({
        detailId: options.id,
        bottomBtnTitle: "更新规则"
      })
      this.queryOneCommissionRuleDetail();
    }
  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
  },
  //多行输入回调绑定:
  multiLineBindInput(e) {
    // console.log("JJLog:输入回调:" + JSON.stringify(e));
    let type = e.currentTarget.dataset.type;
    let changeValue = `formData.${type}`;
    this.setData({
      [changeValue]: e.detail.value
    })
  },
  inputCellChange(e) {
    let type = e.detail.key;
    let changeValue = `formData.${type}`;
    this.setData({
      [changeValue]: e.detail.value
    })
  },

  //查询回填信息:
  queryOneCommissionRuleDetail() {
    wx.showLoading();
    queryOneCommissionRuleDetail(this.data.detailId).then(res => {
      wx.hideLoading();
      console.log("JJLog:回填信息是:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          formData: res.data
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      })
      console.error("JJLog:queryOneCommissionRuleDetail", err);
    })
  },


  //提交佣金规则
  submitCommissionRuleAction() {
    let data = this.data.formData;
    if (!data.name) {
      wx.showToast({
        title: '请输入规则名称',
        icon: "none"
      })
      return;
    }
    console.log("JJLog:提交参数是:" + JSON.stringify(this.data.formData));

    if (this.data.detailId) {
      debounce(this.updateOneCommissionRule);
    } else {
      debounce(this.createOneCommissionRule);
    }

  },

  //创建一个佣金规则:
  createOneCommissionRule() {
    wx.showLoading();
    createOneCommissionRule(this.data.formData).then(res => {
      wx.hideLoading();
      // console.log("JJLog:回填信息是:" + JSON.stringify(res));
      if (res.code == 200) {
        wx.showToast({
          title: '创建成功',
          icon: "success"
        })
        setTimeout(() => {
          wx.navigateBack({
            delta: 0,
          })
        }, 2000);
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "创建失败",
        icon: "none"
      })
      console.error("JJLog:createOneCommissionRule", err);
    })
  },



  //更新佣金规则:
  updateOneCommissionRule() {
    wx.showLoading();
    updateOneCommissionRule(this.data.formData).then(res => {
      wx.hideLoading();
      // console.log("JJLog:回填信息是:" + JSON.stringify(res));
      if (res.code == 200) {
        wx.showToast({
          title: '更新成功',
          icon: "success"
        })
        setTimeout(() => {
          wx.navigateBack({
            delta: 0,
          })
        }, 2000);
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "更新失败",
        icon: "none"
      })
      console.error("JJLog:updateOneCommissionRule", err);
    })
  },

})